package common;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;


public abstract class ClientOperate {

	public Client client;
	
	public abstract void connect();
	/**
	 * 发送请求文件时,发送监听文件变动的 端口
	 * 记录当前文件读取到的位置
	 * 
	 * @param fileName
	 * @param off 是否重头读取 (true continue read curr file)
	 */
	public abstract void getFile(String fileName, boolean off);
	/**
	 * 获得最新共享文件列表.
	 */
	public abstract void getListShareFile();
	/**
	 * client leave.
	 */
	public abstract void sendLogout();
	
	protected synchronized Object sendCmd(Command cmd, Socket socket) {
		
		try {
			ObjectOutputStream cmdOut = new ObjectOutputStream(socket.getOutputStream());
			cmdOut.writeObject(cmd);
			cmdOut.flush();
			ObjectInputStream objectIn = new ObjectInputStream(socket.getInputStream());
			return objectIn.readObject();
		} catch (IOException e) {
			System.out.println("ERROR: can't connect server :[ ip: " + client.getAddress() + ", port: " + client.getPort() + "]");
			System.out.println("Send command ERROR!");
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			System.out.println("ERROR: can't connect server :[ ip: " + client.getAddress() + ", port: " + client.getPort() + "]");
			System.out.println("Send command ERROR!");
			e.printStackTrace();
		}
		return null;
	}
	public Client getClient() {
		return client;
	}
	public void setClient(Client client) {
		this.client = client;
	}
}
